package jp.isisredirect.DreamWaverFirst;

import jp.isisredirect.DreamWaverFirst.Slider.SliderPositionListener;

import android.content.res.Resources;
/*import android.graphics.drawable.Drawable;
import android.text.SpannableStringBuilder;
import android.text.style.RelativeSizeSpan;
import android.text.style.SubscriptSpan;
*/
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

public class PlayerFreqController {
	private int mHeadChNumber;
	private DreamWaverFirstActivity mActivity;
	private Slider mSliders[] = null;

	class FreqValueUpdater implements Slider.SliderPositionListener {
		private TextView mTarget;
		private final byte mCommandTarget;
		private int prevVal;
		private long prevtime;

		FreqValueUpdater(TextView target, int index) {
			mTarget = target;
			mCommandTarget = (byte) (mHeadChNumber + index);
			prevVal = 5;
			prevtime = 0;
		}

		public void onPositionChange(double value) {
		}
	}

	class LabelClickListener implements OnClickListener {
		final private double mValue;
		private final Slider mSlider;

		public LabelClickListener(Slider slider, double value) {
			mSlider = slider;
			mValue = value;
		}

		public void onClick(View v) {
			mSlider.setPosition(mValue);
		}

	}

	public PlayerFreqController(DreamWaverFirstActivity activity, int number, Resources res, boolean vertical) {
		mActivity = activity;
		mHeadChNumber = number;
		mSliders = new Slider[6];
	}

	public void attachToView(ViewGroup targetView) {
		for (int i = 0; i < 6; ++i) {
			ViewGroup g = (ViewGroup) targetView.getChildAt(i);

			TextView label = (TextView) g.getChildAt(0);
			Slider slider = (Slider) g.getChildAt(1);
			TextView valueText = (TextView) g.getChildAt(2);

			SliderPositionListener positionListener = new FreqValueUpdater(valueText, i);
			slider.setPositionListener(positionListener);
			
			LabelClickListener leftLabelListener = new LabelClickListener(slider, 0);
			label.setOnClickListener(leftLabelListener);
			LabelClickListener rightLabelListener = new LabelClickListener(slider, 1);
			valueText.setOnClickListener(rightLabelListener);

			valueText.setText("0");
			
			mSliders[i] = slider;
		}
	}
	public void setPosition(int index, int freq) { 
		if (mHeadChNumber <= index && index < (mHeadChNumber + 6)) {
			if (mSliders[index - mHeadChNumber] != null) {
				double newpos = (1000. / freq) / 30.; // convert

				mSliders[index - mHeadChNumber].setPosition(newpos);
			}
		}
	}
}
